package com.thinkingstar.iads.datacenter.entity.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.IocBean;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.common.utils.CommonDateUtil;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignPhase;
import com.thinkingstar.iads.datacenter.entity.DcProjectFieldWidget;

/**
 * @ClassName: DaoDcProjectFieldWidget 
 * @Description:数据中心_项目_表单控件维护
 * @author 1157372219@qq.com 
 * @date 2018年3月19日   
 */
@IocBean(args = {"refer:dao"})
public class DaoDcProjectFieldWidget extends EntityRepositoryServiceImpl<DcProjectFieldWidget>{
	
	public DaoDcProjectFieldWidget(Dao dao) {
		super(dao);
	}
	
	//查询控件描述
	public DcProjectFieldWidget findWidgetByFieldName(String table_name , String field_name){
		Criteria criteria = Cnd.NEW();
		criteria.where().and("del_flag", "=", false).and("table_name", "=", table_name).and("field_name", "=", field_name);
		List<DcProjectFieldWidget> dcProjectFieldWidget = query(criteria);
		if(!dcProjectFieldWidget.isEmpty()){
			return dcProjectFieldWidget.get(0);
		}
		return null;
	}
	
	
	//查询某表单控件集合
	public List<DcProjectFieldWidget> findWidgetByTableName(String table_name){
		Criteria criteria = Cnd.NEW();
		criteria.where().and("del_flag", "=", false).and("table_name", "=", table_name);
		criteria.getOrderBy().asc("order_num+0");
		List<DcProjectFieldWidget> dcProjectFieldWidget = query(criteria);
		return dcProjectFieldWidget;
	}
	
	//查询某表单是否包含(下拉选,多选框,复选框,单选框任意)
	public Boolean findFormTypeByTableName(String table_name){
		Criteria criteria = Cnd.NEW();
		SqlExpressionGroup e1 = Cnd.exps("widget_type", "like", "%radio%").or("widget_type", "like", "%checkbox%").
				or("widget_type", "like", "%select%").or("widget_type", "like", "%inline_textarea%").or("widget_type", "like", "%inline_text%")
				.or("widget_type", "like", "%image_div%");
		criteria.where().and("del_flag", "=", false).and("table_name", "=", table_name).and(e1);
		List<DcProjectFieldWidget> dcProjectFieldWidget = query(criteria);
		if(dcProjectFieldWidget != null && !dcProjectFieldWidget.isEmpty()){
			return true;
		}
		return false;
	}
	
	public int findWidgetNumByTableName(String table_name){
		Criteria criteria = Cnd.NEW();
		criteria.where().and("del_flag", "=", false).and("table_name", "=", table_name);
		return count(criteria);
	}
	
	//更新非结构化数据表单控件
	public void updateFieldWidget(String table_name , JSONArray jsonArray){
		List<String> del_list = new LinkedList<>();
		List<JSONObject> add_list = new LinkedList<>();
		List<JSONObject> update_list = new LinkedList<>();
		List <DcProjectFieldWidget> findWidgetByTableName = this.findWidgetByTableName(table_name);
		//筛选需要删除的字段
		for(DcProjectFieldWidget object : findWidgetByTableName){
			String origin_field_name = object.getField_name();
			Boolean boolean1 = true; //假设该字段需要删除
			for(int i=0;i<jsonArray.size();i++){
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				String update_field_name = jsonObject.getString("field_name");
				if(origin_field_name.equals(update_field_name)){
					//假设不成立
					boolean1 = false;
					break;
				}
			}
			if(boolean1){
				del_list.add(object.getId());
			}
		}
		//筛选需要添加的字段
		for(int i=0;i<jsonArray.size();i++){
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			String update_field_name = jsonObject.getString("field_name");
			Boolean boolean1 = true; //假设该字段需要添加
			for(DcProjectFieldWidget object : findWidgetByTableName){
				String origin_field_name = object.getField_name();
				if(update_field_name.equals(origin_field_name)){
					boolean1 = false;//假设不成立
					update_list.add(jsonObject);
					break;
				}
			}
			if(boolean1){
				add_list.add(jsonObject);
			}
		}
		//删除某字段控件信息
		for(String del_id : del_list){
			delete(del_id);
		}
		//添加某字段控件信息
		for(JSONObject add_obj : add_list){
			DcProjectFieldWidget addDcProjectFieldWidget = new DcProjectFieldWidget();
			addDcProjectFieldWidget.setTable_name(table_name);
			addDcProjectFieldWidget.setWidget_type(add_obj.getString("widget_type"));
			addDcProjectFieldWidget.setWidget_length(add_obj.getString("widget_length"));
			addDcProjectFieldWidget.setDefault_value(add_obj.getString("form_default"));
			addDcProjectFieldWidget.setField_name(add_obj.getString("field_name"));
			addDcProjectFieldWidget.setField_comment(add_obj.getString("field_comment"));
			addDcProjectFieldWidget.setIs_show(add_obj.getBoolean("is_show"));
			addDcProjectFieldWidget.setCreate_time(CommonDateUtil.getDateTime());
			addDcProjectFieldWidget.setUpdate_time(CommonDateUtil.getDateTime());
			addDcProjectFieldWidget.setOrder_num(add_obj.getString("field_order"));
			insert(addDcProjectFieldWidget);
		}
		//修改某字段控件信息
		for(JSONObject update_obj : update_list){
			String field_name = update_obj.getString("field_name");
			String widget_type = update_obj.getString("widget_type");
			String default_value = update_obj.getString("form_default");
			String widget_length = update_obj.getString("widget_length");
			Boolean is_show = update_obj.getBoolean("is_show");
			DcProjectFieldWidget upDcProjectFieldWidget = this.findWidgetByFieldName(table_name, field_name);
			if(upDcProjectFieldWidget != null){
				upDcProjectFieldWidget.setWidget_type(widget_type);
				upDcProjectFieldWidget.setWidget_length(widget_length);
				upDcProjectFieldWidget.setDefault_value(default_value);
				upDcProjectFieldWidget.setField_comment(update_obj.getString("field_comment"));
				upDcProjectFieldWidget.setUpdate_time(CommonDateUtil.getDateTime());
				upDcProjectFieldWidget.setIs_show(is_show);
				upDcProjectFieldWidget.setOrder_num(update_obj.getString("field_order"));
				update(upDcProjectFieldWidget);
			}
		}
	}
	

}
